# author: alex
# time: 2022/4/26 16:14
"""
    回复测试
"""
import unittest

import ddt
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains

def readFile():
    file = pd.read_csv('data/reply_data.csv')
    df = pd.DataFrame(file).fillna('')
    da=df.to_dict(orient='records')
    return da

@ddt.ddt
class Reply_test(unittest.TestCase):

    def setUp(self) -> None:
        option = webdriver.ChromeOptions()
        option.binary_location = r'E:\Program Files\Google\Chrome\Application\chrome.exe'
        self.driver = webdriver.Chrome(options=option)
        self.url = 'http://localhost:8082/log-reg'
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        self.driver.maximize_window()
        time.sleep(2)
        self.login()

    def login(self):
        username = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(2) > input')
        password = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(3) > input')
        username.send_keys('12345678901')
        password.send_keys('123456')
        btn = self.driver.find_element(By.CSS_SELECTOR, '#form2 > button')
        btn.click()
        time.sleep(2)
        canvas = self.driver.find_element(By.CSS_SELECTOR, 'body > canvas:nth-child(1)')
        ActionChains(self.driver).move_to_element(canvas).move_by_offset(-475, -200).pause(2).click().perform()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, '#homePage > div.RightArctle.details > div:nth-child(1)').click()
        self.driver.find_element(By.CSS_SELECTOR, '#blog > div.do > ul:nth-child(1) > li:nth-child(2)').click()
        time.sleep(1)

    @ddt.data(*readFile())
    def test_reply(self,data):
        self.driver.execute_script('window.scrollBy(0,100)')
        replys1 = self.driver.find_elements(By.CSS_SELECTOR, '#remark > div.msg > div.msg-reply > div')
        self.driver.find_element(By.CSS_SELECTOR, '#remark > div.msg > div.msg-do > span').click()
        replay = self.driver.find_element(By.CSS_SELECTOR, '#sendReply-input')
        replay.send_keys(data.get('reply'))
        reply_btn = self.driver.find_element(By.CSS_SELECTOR, '#sendReply > div.sendReply-do > button')
        reply_btn.click()
        time.sleep(2)
        replys2=self.driver.find_elements(By.CSS_SELECTOR,'#remark > div.msg > div.msg-reply > div')
        if data.get('state')==1:
            self.assertEqual(len(replys1)+1,len(replys2))
        else:
            self.assertEqual(len(replys1), len(replys2))

    def tearDown(self) -> None:
        self.driver.close()

if __name__=='__main__':
    unittest.main()